package com.motu.monstercity.server.game.logichandler;

import com.motu.monstercity.module.common.network.RequestParam;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.CsGameMail.*;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.logic.*;
import com.motu.monstercity.server.game.userdata.UserInfo;
import com.motu.monstercity.server.game.userdata.UserMail;
import com.motu.monstercity.server.game.userdata.UserWeapon;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.toolset.ErrorWord;

import java.util.ArrayList;
import java.util.List;

public class MailHandler {
	// CMD_GAME_MAIL_LIST = 71101; //读取邮件列表
	public static Object CMD_GAME_MAIL_LIST(RequestParam clientParam) throws Exception {
		CSGameMailListRequest request = CSGameMailListRequest.parseFrom(clientParam.getData());
		CSGameMailListResponse.Builder response = CSGameMailListResponse.newBuilder();
		long userId = clientParam.getUserId();
		long dbid = request.getDbid();

		PBUserData.Builder pbUserData = response.getUserBuilder();
		UserInfo userInfo = GameUser.getUserInfo(userId);
		List<UserMail> list = MailManager.getUserMailList(userId);
		for (UserMail userMail : list)  {
			if (userMail.isTimeExpire()) {
				userMail.delete();// 删除过期的邮件
			} else {
				if (userMail.getId() > dbid)  {
					pbUserData.getMailBuilder().putMailList(userMail.getId(), userMail.takeInitBuilder(userInfo.getLanguage()).build());
				}
			}
		}
		return response;
	}

	// CMD_GAME_MAIL_GET = 71102; //领取邮件附件
	public static Object CMD_GAME_MAIL_GET(RequestParam clientParam) throws Exception {
		CSGameMailGetRequest request = CSGameMailGetRequest.parseFrom(clientParam.getData());
		CSGameMailGetResponse.Builder response = CSGameMailGetResponse.newBuilder();
		long userId = clientParam.getUserId();
		long dbid = request.getDbid();
		boolean isDel = request.getDel();

		if (dbid == 0 && isDel) {
			return ErrorWord.WRONG_STATUS;// 只能删除一封
		}

		List<UserMail> list = new ArrayList<>();
		int weaponNum = PartnerManager.countWeaponNum(userId);// 当前的武器数量
		if (dbid == 0) {
			for (UserMail userMail : MailManager.getUserMailList(userId)) {
				int mailWeaponNum = userMail.getWeaponNum();// 这个邮件里附件里的武器数量
				if (userMail.hasItems() && !userMail.isGet() && !userMail.isImportant()) {// 有奖励,未领取,不是重要邮件
					list.add(userMail);
				}
			}
		} else {
			UserMail userMail = MailManager.getUserMail(userId, dbid);
			if (userMail == null) {
				return ErrorWord.ENTITY_NOT_EXIST;
			}
			// 是否有附件
			if (!userMail.hasItems()) {
				return ErrorWord.INVALID_OPERATION;
			}
			// 已经领取过了
			if (userMail.isGet()) {
				return ErrorWord.WRONG_STATUS;
			}
			if (userMail.isOnlyWeapon() && (weaponNum  >= AllParam.WEAPON_NUM_MAX)) {
				return ErrorWord.ALREADY_MAX;// 武器数量达到上限了
			}
			list.add(userMail);
		}

		// 执行逻辑
		UserInfo userInfo = GameUser.getUserInfo(userId);
		PBUserData.Builder pbUserData = response.getUserBuilder();
		for (UserMail userMail : list) {
			List<RewardItem> rewardItems = userMail.takeRewardItem(PartnerManager.countWeaponNum(userId));// 武器数量会增加，重新获取一次
			RewardManager.addReward(userInfo, rewardItems, pbUserData, userMail.takeLogType());
			if (userMail.getIsSomeReward()) {// 只领取了部分奖励，奖励内容有变动
				userMail.update();
				pbUserData.getMailBuilder().putMailList(userMail.getId(), userMail.takeInitBuilder(userInfo.getLanguage()).build());
			} else {
				if (isDel) {
					userMail.delete();
					ProtoDataUtils.deletePBUserData(pbUserData, userMail);
				} else {
					userMail.putIsRead((byte) 1);
					userMail.putIsGet((byte) 1);
					userMail.update();
					ProtoDataUtils.updatePBUserData(pbUserData, userMail);
				}
			}
		}
		
		return response;
	}

	// CMD_GAME_MAIL_DEL = 71103; //删除邮件
	public static Object CMD_GAME_MAIL_DEL(RequestParam clientParam) throws Exception {
		CSGameMailDelRequest request = CSGameMailDelRequest.parseFrom(clientParam.getData());
		CSGameMailDelResponse.Builder response = CSGameMailDelResponse.newBuilder();
		long userId = clientParam.getUserId();
		long dbid = request.getDbid();

		PBUserData.Builder pbUserData = response.getUserBuilder();
		if (dbid == 0) {// 删除所有邮件,重要邮件,未读邮件,未领取奖励的不能删除
			List<UserMail> list = MailManager.getUserMailList(userId);
			for (UserMail userMail : list) {
				if (userMail.isImportant() || !userMail.isRead()) {
					continue;// 重要邮件不能批量删除,未读邮件也不能删除
				}
				if (userMail.hasItems() && !userMail.isGet()) {// 有附件未领取不可以删除
					continue;
				}
				userMail.delete();
				ProtoDataUtils.deletePBUserData(pbUserData, userMail);
			}
		} else {
			UserMail userMail = MailManager.getUserMail(userId, dbid);
			if (userMail == null) {
				return ErrorWord.ENTITY_NOT_EXIST;
			}
			if (userMail.hasItems() && !userMail.isGet()) {// 有附件未领取不可以删除
				return ErrorWord.WRONG_STATUS;
			}
			userMail.delete();
			ProtoDataUtils.deletePBUserData(pbUserData, userMail);
		}

		return response;
	}

	// CMD_GAME_MAIL_READ = 71104; //读邮件
	public static Object CMD_GAME_MAIL_READ(RequestParam clientParam) throws Exception {
		CSGameMailReadRequest request = CSGameMailReadRequest.parseFrom(clientParam.getData());
		CSGameMailReadResponse.Builder response = CSGameMailReadResponse.newBuilder();
		long userId = clientParam.getUserId();
		long dbid = request.getDbid();

		PBUserData.Builder pbUserData = response.getUserBuilder();
		UserMail userMail = MailManager.getUserMail(userId, dbid);
		if (userMail == null) {
			return ErrorWord.ENTITY_NOT_EXIST;
		}
		if (userMail.isRead()) {
			return ErrorWord.REPEATED_OPERATION;
		}
		userMail.putIsRead((byte) 1);
		userMail.update();
		ProtoDataUtils.updatePBUserData(pbUserData, userMail);

		return response;
	}
}
